<?php
namespace Crawl\Controller;
use Common\Controller\AdminbaseController;
use Think\Cache\Driver\Redis;
// +----------------------------------------------------------------------
// | Date:2018年1月23日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class RedisConfigController extends AdminbaseController{
    protected $redis;
    protected $redis_key;
    public function _initialize(){
        $this->redis = new Redis();
        $this->assign('title','配置信息') ;
        $this->redis_key = C('REDIS_PREFIX').'config';
        
    }
    
    public function setconfig(){
        $key = I('key');
        $val = I('val');
        $ret = $this->redis->hSet($this->redis_key,$key,$val);
        $this->success('操作成功！');
        
    }
    
    public function crawl_switch(){
        $val = I('val');
        $open = 'monitor_public_opinion/run.py';
        $curl = new \Curl();
        
        if ($val == 1) {
            //开启
           // $bash = "nohup python /home/wwwroot/h5.idea.3xy.me/insight_crawl2018/monitor_public_opinion/$open &";
//            $bash = "php /home/wwwroot/h5.idea.3xy.me/insight_crawl2018/run_mop.php";
        }else{
//             $bash = "kill -9 $(ps -aux |grep '{$open}'|grep -v grep|awk '{print $2}')";
        }
//        $output = '';
//        $output = shell_exec($bash);
//        $this->success('操作完成');
    }
    
    
    public function index(){
        $list = $this->redis->hGetAll($this->redis_key);
        //状态检查
        $output = '';
        $open = 'monitor_public_opinion/run.py';
        $bash = "ps -aux |grep '{$open}'|grep -v grep|awk '{print $2}'";
        $output = shell_exec($bash);
        if ($output) {
            $crawl_status = ['code'=>1,'info'=>'正在进行...'];
        }else{
            $crawl_status = ['code'=>0,'info'=>'关闭'];
        }
        $this->assign('crawl_status',$crawl_status);        
        $this->assign('list',$list);
        $this->display();
    }
    
}